package com.pj.android.brower.business;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.webkit.JavascriptInterface;

import com.pj.android.brower.MyApplication;
import com.pj.android.brower.business.bean.Location;
import com.pj.android.brower.common.LocationManager;
import com.pj.android.brower.ui.WebActivity;
import com.pj.android.brower.util.Config;
import com.pj.android.brower.util.SPUtils;
import com.pj.android.relief.base.log.Logger;

/**
 * Create By hHui on 2018/9/27
 * <p>
 * Android 提供给js调用的方法
 */
public class AndroidForJs {
    private String tag = "AndroidForJs";

    WebActivity activity;

    public AndroidForJs(WebActivity activity) {
        this.activity = activity;
    }

    /**
     * 加载新的页面
     *
     * @param url
     */
    @JavascriptInterface
    public void loadUrl(String url) {
        Logger.dft().i(tag, url);
        Intent intent = new Intent(activity, WebActivity.class);
        intent.putExtra("url", url);
        activity.startActivity(intent);
    }

    /**
     * 显示状态栏
     *
     * @param title
     */
    @JavascriptInterface
    public void showTitle(String title) {
        activity.showTitle(title);
    }

    /**
     * 隐藏状态栏
     */
    @JavascriptInterface
    public void hiddenTitle() {
        activity.hiddenTitle();
    }

    /**
     * 存值
     *
     * @param key
     * @param value
     */
    @JavascriptInterface
    public void save(String key, String value) {
        SPUtils.Companion.getInstance().put(key, value, true);
    }

    /**
     * 取值
     *
     * @param key
     * @return
     */
    @JavascriptInterface
    public String query(String key) {
        return SPUtils.Companion.getInstance().getString(key, "");
    }

    /**
     * 获取 device_token
     *
     * @return
     */
    @JavascriptInterface
    public String queryDeviceToken() {
        Logger.dft().i(tag, "网页调用 queryDeviceToken");
        return Config.Companion.getInstance().getDeviceToken();
    }

    /**
     * 扫描二维码
     *
     * @param callbackName 回调js方法名字
     */
    @JavascriptInterface
    public void scan(String callbackName, String sn) {
        Logger.dft().i(tag, String.format("网页调用 scan(%s,%s)", callbackName, sn));
        activity.scanQrCode(callbackName, sn);
    }

    /**
     * 返回上一页
     *
     * @param funName
     * @param params
     */
    @JavascriptInterface
    public void back(String funName, String[] params) {
        activity.finish();
        activity.runJs(funName, params);
    }

    /**
     * 清理之前所有打开的页面
     */
    @SuppressLint("NewApi")
    @JavascriptInterface
    public void clean() {
        MyApplication.cleanBeforeActivity();
    }

    /**
     * 写日志
     *
     * @param message
     */
    @JavascriptInterface
    public void log(String message) {
        Logger.dft().i(tag, message);
    }

    /**
     * 获取当前位置
     *
     * @return
     */
    @JavascriptInterface
    public void getNowLocation(String callBackName) {
        LocationManager manager = new LocationManager(activity);
        manager.start(callBackName);
    }

    /**
     * 拨打电话
     *
     * @param phoneNumber
     */
    @JavascriptInterface
    public void callPhone(String phoneNumber) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:" + phoneNumber));
        activity.startActivity(intent);
    }
}
